<?php
/**
 * @since 08.07.2011
 * @author Akulov Denis <akulov.d.g@gmail.com>
 */
class MVC_View implements MVC_View_Interface{

    private $_arView = array ();


    private $_arVariables = array ();


    private $_iIndex = -1;

    /**
     * @var Config_Interface
     */
    private $_objConfig = null;


    public function __construct(Config_Interface $objConfig) {
        $this->_objConfig = $objConfig;
    }


    public function addView ($strViewName) {
        $this->_iIndex++;
        $this->_arView[] = $strViewName;
    }


    public function addVariable ($strVarName, $mValue) {
        $this->_arVariables[$this->_iIndex][$strVarName] = $mValue;
    }


    public function parse () {
        $strResult = '';
        ob_start();
        $i = -1;
        foreach ($this->getView() as $view) {
            $i ++;
            $this->addSubview($view, $this->getVariables($i));
        }
        $strResult = ob_get_contents();
        ob_end_clean();
        return $strResult;
    }


    private function addSubview($view, $values = array()) {
        foreach ($values as $name => $value) {
            $$name = $value;
        }
        require $this->getConfig()->getAttribute('FW_PATHS', 'PROTOTYPE').
                'MVC/V/'.str_replace('_','/',$view).'.php';
    }


    private function getView () {
        return $this->_arView;
    }


    /**
     * @return Config_Interface
     */
    private function getConfig () {
        return $this->_objConfig;
    }


    private function getVariables ($iIndex) {
        return $this->_arVariables[$iIndex];
    }
}